﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace StockCommon.data
{
    public class TradeInstManager
    {
        protected static Dictionary<String, Inst> code2Insts = null;
        public static void preloadInst()
        {
            if (code2Insts == null)
            {
                StockDataClassesDataContext context = LinqAgent.getContext();
                code2Insts = (from x in context.Insts select x).ToDictionary(x => x.InstCode);
            }
        }
        public static bool getInstId(String code, ref  Guid result)
        {
            code = code.Trim();
            if (code2Insts == null)
                preloadInst();
            if (code2Insts.ContainsKey(code))
            {
                result = code2Insts[code].InstId;
                return true;
            }
            return false;
        }
        public static Guid registerInst(String code, String name)
        {
            code = code.Trim();
            if (code2Insts == null)
                preloadInst();
            if (code2Insts.ContainsKey(code))
                return code2Insts[code].InstId; ;


            Inst inst = new Inst();
            inst.InstCode = code;
            inst.InstName = name;
            inst.InstId = NewGuid.create("inst");
            StockDataClassesDataContext context = LinqAgent.getContext();
            context.Insts.InsertOnSubmit(inst);
            context.SubmitChanges();
            code2Insts.Add(code, inst);
            return inst.InstId;
        }

    }
}
